﻿import { Component, EventEmitter, Injector, Output, ViewChild, Input } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { OrganizationUnitDto, PasswordComplexitySetting, ProfileServiceProxy, CheckerRoleEnum, UserRoleDto } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import { RoleUserServiceProxy,RoleUserDto} from '@shared/service-proxies/service-proxies';
@Component({
    selector: 'auditAuthorityTabEditComponent',
    templateUrl: './audit-authority_tab_edit.component.html'
})
export class AuditAuthorityTabEditComponent extends AppComponentBase {
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    canChangeUserName = true;
    isTwoFactorEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.TwoFactorLogin.IsEnabled');
    isLockoutEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.UserLockOut.IsEnabled');
    passwordComplexitySetting: PasswordComplexitySetting = new PasswordComplexitySetting();

    record: RoleUserDto = new RoleUserDto();
    roles: UserRoleDto[];
    sendActivationEmail = true;
    setRandomPassword = true;
    passwordComplexityInfo = '';
    profilePicture: string;

    allOrganizationUnits: OrganizationUnitDto[];
    memberedOrganizationUnits: string[];
    editFlag = true;
    roleOption:object[];
    allUserOption:Array<object>;

    constructor(
        injector: Injector,
        private _roleUserServiceProxy:RoleUserServiceProxy,
        private _profileService: ProfileServiceProxy
    ) {
        super(injector);
 
        this.roleOption = new Array<object>()
        this.roleOption.push({value:0,text:CheckerRoleEnum[0]});
        this.roleOption.push({value:1,text:CheckerRoleEnum[1]});
        this.roleOption.push({value:2,text:CheckerRoleEnum[2]});
        this.roleOption.push({value:3,text:CheckerRoleEnum[3]});

        this.getAllUserOption();
    }
    getAllUserOption() {
        this.allUserOption = new Array<object>()
        this._roleUserServiceProxy.getAllUsers()
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.allUserOption = result;
            });
    }
    show(_pUoleUserDto: RoleUserDto, index: number): void {
        if (_pUoleUserDto) {
            this.record = _pUoleUserDto;
            //this.editFlag = false;
        } else {
            this.record = new RoleUserDto();
            this.record.role = index;
        }
        this.active = true;
        this.modal.show();
    }

    onShown(): void {
        document.getElementById('id').focus();
    }

    save(): void {
        this._roleUserServiceProxy.createUserOfRole(this.record)
        .pipe(finalize(() => { this.saving = false; }))
        .subscribe(() => {
            this.notify.info(this.l('SavedSuccessfully'));
            this.close();
            this.modalSave.emit(null);
        });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
